package com.ruoyi.customerSample.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.customerSample.domain.SampleDetailBill;
import com.ruoyi.customerSample.service.ISampleDetailBillService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 样品接收明细Controller
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/sample/detailBill")
@Api(value="样品接收明细",tags = "样品接收明细接口")
public class SampleDetailBillController extends BaseController
{
    @Autowired
    private ISampleDetailBillService sampleDetailBillService;

    /**
     * 查询样品接收明细列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询样品接收明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(SampleDetailBill sampleDetailBill)
    {
        startPage();
        List<SampleDetailBill> list = sampleDetailBillService.selectSampleDetailBillList(sampleDetailBill);
        return getDataTable(list);
    }

    /**
     * 导出样品接收明细列表
     */
    @Log(title = "样品接收明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleDetailBill sampleDetailBill)
    {
        List<SampleDetailBill> list = sampleDetailBillService.selectSampleDetailBillList(sampleDetailBill);
        ExcelUtil<SampleDetailBill> util = new ExcelUtil<SampleDetailBill>(SampleDetailBill.class);
        return util.exportExcel(list, "bill");
    }

    /**
     * 获取样品接收明细详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取样品接收明细详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sampleDetailBillService.selectSampleDetailBillById(id));
    }

    /**
     * 获取当前最大样品编号
     */
    @GetMapping("/getMaxSampleNum/{type}/{sampleNum}")
    @ApiOperation(value="获取当前最大样品编号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "type", value = "类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "sampleNum", value = "样品号", required = true, dataType = "String"),
    })
    public AjaxResult getMaxSampleNum(@PathVariable("type") String type, @PathVariable("sampleNum") String sampleNum) {
        //依据不同类别判定编号长度
        String maxNum = null;
        if(StringUtils.equals(type, "YB")) {
            //仪表长度为4
            maxNum = sampleDetailBillService.selectsampleNumByType("4", sampleNum);
            if(StringUtils.isEmpty(maxNum)) {
                maxNum = "0000";
            }
        }else {
            maxNum = sampleDetailBillService.selectsampleNumByType("3", sampleNum);
            if(StringUtils.isEmpty(maxNum)) {
                maxNum = "000";
            }
        }
        return AjaxResult.success(maxNum);
    }

    /**
     * 新增样品接收明细
     */
    @Log(title = "样品接收明细", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增样品接收明细")
    public AjaxResult add(@ApiParam(name="样品接收明细",value="传入json格式",required=true)@RequestBody SampleDetailBill sampleDetailBill)
    {
        return toAjax(sampleDetailBillService.insertSampleDetailBill(sampleDetailBill));
    }

    /**
     * 修改样品接收明细
     */
    @Log(title = "样品接收明细", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改样品接收明细")
    public AjaxResult edit(@ApiParam(name="样品接收明细",value="传入json格式",required=true)@RequestBody SampleDetailBill sampleDetailBill)
    {
        return toAjax(sampleDetailBillService.updateSampleDetailBill(sampleDetailBill));
    }

    /**
     * 删除样品接收明细
     */
    @Log(title = "样品接收明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除样品接收明细")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sampleDetailBillService.deleteSampleDetailBillByIds(ids));
    }
}
